let app = getApp();

var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      pro_title:"请选择项目",
      staff:"",
      show:false,
      keyword:'',
      projectList:[],
      defineProject:'',
      dictionaries:[],
      personnels:[],
      selDictionariesName:'',
      selDictionariesId:0,
      personnelName:'',
      personnelId:0,
      accountList:[],  //协助人员
      pro_id:0,  //项目id
      content:"", //内容
      phone:"",//联系电话
      time:"2021-09-09",
      dictionariesKeyword:"",
      dictionariesIndex:-1,
      personnelsKeyword:"",
      personnelsIndex:-1,
  },

  showView:function(){
    var show = !this.data.show;
    this.setData({show:show,dictionariesKeyword:'',dictionariesIndex:-1,personnelsKeyword:'',personnelsIndex:-1});
    this.dictionariesApi();
    this.dictionariesClick();
},

    //获取责任单位
    dictionariesApi:function(){
      var keyword = this.data.dictionariesKeyword;
      let params = {
        appid: app.globalData.appid,
        keyword:keyword
      }
      app.request('post', '/api/help/dictionaries', params).then(res => {
        let dictionaries = res.data.list;
        this.setData({dictionaries:dictionaries});
      }).catch(err => console.log(err));
      this.setData({personnels:[],dictionariesIndex:-1});
    },

    //选择责任单位
  onClickDictionaries:function(e){
    var dictionaries = this.data.dictionaries;
    var dictionariesIndex = e.detail.value;
    var selDictionariesName = dictionaries[dictionariesIndex].name;
    var selDictionariesId = dictionaries[dictionariesIndex].id;
    //查询人员
    let params = {
      appid: app.globalData.appid,
      dictionariesId:selDictionariesId
    }
    app.request('post', '/api/help/personnel', params).then(res => {
      let personnels = res.data.list;
      this.setData({personnels:personnels});
    }).catch(err => console.log(err));
    //dictionaries:[],
    this.setData({selDictionariesName:selDictionariesName,selDictionariesId:selDictionariesId,dictionariesIndex:dictionariesIndex});
},

//获取责任单位下面的相关人员
dictionariesClick:function(){
  var keyword = this.data.personnelsKeyword;

  var dictionaries = this.data.dictionaries;
  var dictionariesIndex = this.data.dictionariesIndex;
  if(dictionariesIndex!=-1){
    var selDictionariesId = dictionaries[dictionariesIndex].id;
  }else{
    var selDictionariesId = 0;
  }

  // var selDictionariesId = this.data.selDictionariesId;
  // dictionariesId:selDictionariesId,
  let params = {
    appid: app.globalData.appid,
    keyword:keyword,
    dictionariesId:selDictionariesId
  }
  app.request('post', '/api/help/personnel', params).then(res => {
    let personnels = res.data.list;
    this.setData({personnels:personnels});
  }).catch(err => console.log(err));
},

//选择人员
onClickPersonnel:function(e){
  //获取人员信息
  var personnels = this.data.personnels;
  var personnelsIndex = e.detail.value;

  var id = personnels[personnelsIndex].id;
  var params = {
    id:id
  };
  var self = this;
  app.request('post', '/api/help/personnelDetail', params).then(res => {
    let detail = res.data.detail;

     // 人员姓名
    var personnelName = detail.name;  
    // 人员id
    var personnelId = detail.id;  
    //所属单位
    var selDictionariesName = detail.selDictionariesName;
    var accountList = self.data.accountList;
    var addAuto = 1;
    for (let i = 0; i < accountList.length; i++) {
      if(accountList[i].personnelId==personnelId){
        addAuto = 0;
        break;
      }
    }
    if(addAuto==1){
      var addTo = {personnelName:personnelName,personnelId:personnelId,selDictionariesName:selDictionariesName};
      self.data.accountList.unshift(addTo);
    }
    self.setData({
      accountList: self.data.accountList
    });


  }).catch(err => console.log(err));


},



  //选择日期
  bindTimeChange:function(e){
    var time = e.detail.value
    this.setData({time:time});
  },

  //内容录入
  inputArea:function(e){
      var content = e.detail.value
      this.setData({content:content});
  },

  //确定项目操作
  onClickDetail:function(e){
    var pro_id = e.currentTarget.dataset.id;
    var defineProject = e.currentTarget.dataset.name;
    this.setData({keyword:'',projectList:[],defineProject:defineProject,pro_id:pro_id});
  },

  //清空操作
  searachRemove:function(e){
      this.setData({keyword:'',projectList:[],defineProject:'',pro_id:0});
  },
  //搜索我所关联的项目
  searachApi:function(e){
    var keyword = e.detail.value;
    this.setData({keyword:keyword});
    var keyword = this.data.keyword;

    if(keyword!=''){
        var params = {keyword:keyword,searach:1};
        //获取项目列表
        app.request('post', '/api/Project/listApi', params).then(res => {
           let projectList = res.data.list;
           this.setData({projectList:projectList});
       }).catch(err => console.log(err));
    }else{
        this.setData({keyword:'',projectList:[]});
    }
  },


  //删除人员
  removePerson:function(e){
    var accountList = this.data.accountList;
    var index = e.currentTarget.dataset.index
    accountList.splice(index,1);
    this.setData({
      accountList: this.data.accountList
    });

  },



    /** 取消 */
  onCancel(){
      wx.navigateBack();
  },

  //输入号码
  inputPhone:function(e){
    this.data.phone = e.detail.value;
  },


  //提交
  onSumit(){
    var accountList = JSON.stringify(this.data.accountList);
    var pro_id = this.data.pro_id;
    var content = this.data.content;
    var time = this.data.time;
    var phone = this.data.phone;

    let params = {
      appid: app.globalData.appid,
      pro_id:pro_id,sumbit_time:time,content:content,accountId:accountList,
      phone:phone
    }
    app.request('post', '/api/help/createHelp', params).then(res => {
      if(res.data.code==0){
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 1500,
          success:function(){
            wx.navigateBack();
          },
        });
        
      }
    }).catch(err => console.log(err));

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var TIME = util.formatTime(new Date());
    this.setData({time:TIME});
    this.dictionariesApi();
    this.dictionariesClick();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})